package client.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import client.MusicClient;
import client.Service;

public class MusicPanel extends DisplayPanel implements ActionListener, MouseListener {
	private static final long serialVersionUID = -8844414678998615696L;
	private final JButton ArtistList = new JButton("Get Artist List");
	private final JButton SearchArtist = new JButton("Search by Artist");
	private final JTextField search = new JTextField("Search Field");
	private final JPanel displayPanel = new JPanel();
	private JScrollPane scroll;
	private ArrayList<ItemPanel> items;
	public MusicPanel(Service s) {
		super(s);
		items = new ArrayList<ItemPanel>();
		init();
	}
	private void init(){
		add(ArtistList);
		ArtistList.addActionListener(this);
		add(SearchArtist);
		SearchArtist.addActionListener(this);
		add(search);
		search.addActionListener(this);
		displayPanel.setPreferredSize(new Dimension(750, 5000));
		scroll = new JScrollPane(displayPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.getVerticalScrollBar().setUnitIncrement(50);
		scroll.setPreferredSize(new Dimension(750,500));
		add(scroll);
	}
	
	public void actionPerformed(ActionEvent arg0) {
		displayPanel.removeAll();
		items.clear();
		if(arg0.getSource() == ArtistList){
			ArrayList<String> s = ((MusicClient) myClient).getArtistList();
			removeAllItems();
			for (String string : s) {
				ArtistPanel ap =new ArtistPanel(string);
				addItem(ap);
			}
			updateUI();
		} else if(arg0.getSource() == SearchArtist){
			ArrayList<String> s = ((MusicClient) myClient).searchArtist(search.getText());
			removeAllItems();
			for (String string : s) {
				ArtistPanel ap =new ArtistPanel(string);
				addItem(ap);
			}
			updateUI();
		}		
	}
	
	public void removeAllItems(){
		displayPanel.removeAll();
		items.clear();
	}

	public void addItem(ItemPanel i){
		items.add(i);
		displayPanel.add(i);
		i.setML(this);
	}
	
	public void mouseClicked(MouseEvent arg0) {
		if(arg0.getSource().getClass().equals(ArtistPanel.class)){ // when an ArtistPanel is Clicked on
			ArtistPanel source = (ArtistPanel)arg0.getSource();
			ArrayList<String> s = ((MusicClient) myClient).getAlbumList(source.getName());
			removeAllItems();
			addItem(source);
			for (String string : s) {
				addItem(new AlbumPanel(source.getName(),string));
			}
			updateUI();
		}
		else if (arg0.getSource().getClass().equals(AlbumPanel.class)){
			AlbumPanel source = (AlbumPanel)arg0.getSource();
			ArrayList<String> s = ((MusicClient) myClient).getTrackList(source.getArtist(), source.getAlbum());
			removeAllItems();
			addItem(new ArtistPanel(source.getArtist()));
			addItem(source);
			for (String string : s) {
				addItem(new TrackPanel(source.getArtist(),source.getAlbum(), string));
			}
			updateUI();

		} else if(arg0.getSource().getClass().equals(TrackPanel.class)){
			TrackPanel source = (TrackPanel)arg0.getSource();
			((MusicClient) myClient).playTrack(source.getArtist(), source.getAlbum(), source.getTrack(), source);
			updateUI();
		}
	}
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mousePressed(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {}
}
